using System;
using System.Net.Sockets;
namespace fuzzyKittenVNC
{
	namespace VNCSecurity {
		public class SecNone : VNCSecurity.SecImplementation
		{
			public SecNone ()
			{	
			}
			protected Socket socket = null;
			protected VNCClient.RFB_PROTOCOL_TYPE protoVersion;
			public int getHandlerNumber() {
				return 1;
			}
			
			public void setPassword(string pass) {}  
			
			public void setup(Socket socket,VNCClient.RFB_PROTOCOL_TYPE protoVersion) {
				this.socket = socket;
				this.protoVersion = protoVersion;
				switch(this.protoVersion) {
					case VNCClient.RFB_PROTOCOL_TYPE.RFB_3_3:	
					case VNCClient.RFB_PROTOCOL_TYPE.RFB_3_7:
						this.sendProtocolNr();
						break;
					case VNCClient.RFB_PROTOCOL_TYPE.RFB_3_8:
						this.sendProtocolNr();	
						this.getSecurityResult();
						break;
				}
				Console.WriteLine("New connection with security type 'None' established!");
			}
			

			
			public void sendProtocolNr() {
				byte[] nr = { Convert.ToByte(this.getHandlerNumber())};
				this.send(nr);
			}
			
			public void getSecurityResult() {
				byte[] securityResult = new byte[4];
				this.receive(securityResult);
				if(!this.socket.Connected)
					throw new VNCSecurityTypeException();
			}
			
			public void send(byte[] msg) {
				string txt = "";
				foreach(byte msgChar in msg) {
					txt += Convert.ToInt32(msgChar)+"|";
				}
				if(!this.socket.Connected) 
					throw new VNCConnectionException();
				socket.Send(msg,msg.Length,SocketFlags.None);
			}
			
			public int receive(byte[] msg) {
				return socket.Receive(msg);
			}
		}
	}
}

